import { createApp } from 'vue'
import App from './App.vue'
import store from './store'
import { router, addMenuRoutes } from './router'
import { useRuntime } from './hooks/system/useGlobal'
// import * as globalComponents from './components'

// echarts按需引入
import './assets/js/echartsInit.js'

// 重置样式
import 'normalize.css'
import './assets/styles/reset.scss' // 引入自定义重置样式

const app = createApp(App)

// 安装store
app.use(store)

// 安装菜单路由
addMenuRoutes(router)
app.use(router)

// 挂载全局组件
// Object.keys(globalComponents).forEach((key) => {
//   app.component(key, globalComponents[key])
// })

// 读取public/runtime/config.js配置文件
const { getRuntimeConfig } = useRuntime()
getRuntimeConfig().then((res) => {
  const module = {}
  try {
    const fn = eval(`((module) => {
      ${res}
    })`)
    fn(module)
    const $config = module.export()
    app.config.globalProperties.$config = $config
    document.title = $config.systemTitle
    app.mount('#app')
  } catch (e) {
    document.write(`<span style="color:red;">解析配置文件失败！请联系管理员。</span>`)
    throw new Error('解析配置文件失败！请联系管理员。')
  }
})
